package com.taxi.driver.module.account.newpwd;

import android.app.Activity;
import android.text.TextUtils;

import com.qianxx.utils.RxUtil;
import com.qianxx.utils.encode.RandomUtil;
import com.qianxx.utils.text.RegUtils;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.user.UserRepository;

import javax.inject.Inject;

/**
 * 功能描述：
 */
public class NewPwdPresenter extends BasePresenter implements NewPwdContract.Presenter {

    UserRepository mUserRepository;
    NewPwdContract.View mView;

    @Inject
    public NewPwdPresenter(UserRepository userRepository, NewPwdContract.View view) {
        mUserRepository = userRepository;
        mView = view;
    }

    @Override
    public void resetPwd(String mobile, String identifyCode, String idcard, String password) {
            if (TextUtils.isEmpty(password)) {
                mView.toast("请输入密码");
                return;
            }
            if (password.length() < 8) {
                mView.toast("密码最少8位");
                return;
            }
            if (!RegUtils.isStringFormatCorrectNew(password)) {
                mView.toast(((Activity) mView).getResources().getString(R.string.hint4));
                return;
            }


        mSubscriptions.add(
                mUserRepository.resetPwd(RandomUtil.RandomEncrypt(mobile), RandomUtil.RandomEncrypt(identifyCode),
                        RandomUtil.RandomEncrypt(idcard), RandomUtil.RandomEncrypt(password), mUserRepository.getDriverType())
                        .compose(RxUtil.applySchedulers())
                        .doOnSubscribe(() -> mView.showLoadingView(true))
                        .doAfterTerminate(() -> mView.hideLoadingView())
                        .subscribe(s -> {
                            mView.toast("密码修改成功，请重新登录");
                            mView.resetSuccess(mUserRepository.getTempDriverEntity() != null);
                        }, ex -> {
                            showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                        }));
    }

}
